package com.dingding.web.coupon.manager;

import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dingding.common.core.util.PropertiesUtil;
import com.dingding.common.core.util.tools.QR.QRTool;
import com.dingding.facde.activity.dto.UseCouponInfoDTO;
import com.dingding.web.base.action.BaseAction;

/**
 * @ClassName: ManageCoupon
 * @Description: 优惠券管理action
 * @author fengshuonan
 * @date 2016年2月4日 上午11:24:34
 * @version 叮叮搭车2.0
 */
@SuppressWarnings("serial")
public class ManageCoupon extends BaseAction {

	// 门店管理员登录后获取优惠券详细信息封装bean
	private UseCouponInfoDTO useCouponInfo;

	HttpSession session = ServletActionContext.getRequest().getSession();

	
	/**
	 * @Title: createQR
	 * @Description: 生成二进制扫描码
	 * @author fengshuonan
	 * @return String
	 * @throws
	 */
	public String createQR()
	{
		try {
			QRTool.createQr(getPara("couponId"), response.getOutputStream());
		} catch (IOException e) {
			log.error("创建二维码错误！ManageCoupon：createQR()",e);
			e.printStackTrace();
		}
		
		return NONE;
	}
	
	/**
	 * @Title: scanQR 
	 * @Description:扫描二维码后跳转到的网页 
	 * @author fengshuonan
	 */
	public String scanQR() {

		request.setAttribute("couponId", getPara("couponId"));

		setPara("couponId", getPara("couponId"));

		return "loginPage";
	}

	/**
	 * @Title: login 
	 * @Description:优惠券管理员登录action 
	 * @author fengshuonan 
	 */
	public String login() {

		useCouponInfo = super.storeManagerService.managerLogin(getPara("tellphone"), getPara("password"),
				getPara("couponId"));

		setPara("couponId", getPara("couponId"));

		if (useCouponInfo != null) {
			session.setAttribute("userId", useCouponInfo.getUserId());
			return "loginSuccess";
		} else {
			setPara("errorMsg", "wrong_pwd");
			return "loginFail";
		}
	}

	/**
	 * @Title: ensureUseCoupon 
	 * @Description: 确认使用优惠券 
	 * @author fengshuonan
	 */
	public String ensureUseCoupon() {

		String userId = (String) session.getAttribute("userId");
		
		boolean flag = super.couponService.useCoupon(getPara("couponId"),userId);

		if (flag) {
			request.setAttribute("messageContent", "优惠券消费成功！");
			request.setAttribute("icon", "weui_icon_success");
			return "useCouponResult";
		} else {
			request.setAttribute("messageContent", "优惠券消费失败！");
			request.setAttribute("icon", "weui_icon_warn");
			return "useCouponResult";
		}

	}

	/**
	 * @Title: backPassword 
	 * @Description: 找回密码-获取验证码 
	 * @author fengshuonan 
	 */
	public String getVerifyCode() {

		// 手机号
		String tellphone = getPara("tellphone");

		boolean flag = super.phonevalidateService.getVerifyCode(tellphone);

		if (flag) {
			return SUCCESS;
		}

		return ERROR;
	}

	/**
	 * @Title: verifyCode 
	 * @Description: 找回密码-验证验证码是否正确 
	 * @author YHQ 
	 */
	public String verifyCode() {

		// 电话号码
		String tellphone = getPara("tellphone");
		// 验证码
		String verifyCode = getPara("verifyCode");

		boolean flag = super.phonevalidateService.verifyCode(tellphone, verifyCode);

		if (flag) {

			session.setAttribute("phone", tellphone);

			return "verifySuccess";
		}

		return "verifyError";
	}

	/**
	 * @Title: changePassword 
	 * @Description:修改密码 
	 * @author fengshuonan 
	 */
	public String changePassword() {

		// 用户手机号
		String tellphone = session.getAttribute("phone").toString();
		// 新密码
		String password = getPara("pwd");

		boolean flag = super.storeManagerService.managerForgetPassword(tellphone, password);

		if (flag) {
			return "changPwdSuccess";
		}

		return "changPwdError";
	}
	
	
	/**
	 * @Title: checkAppVersion
	 * @Description: 获取app内集成的html5+模块当前版本号
	 * @author fengshuonan
	 * @return String
	 * @throws
	 */
	public String checkAppVersion(){
		
		super.write(PropertiesUtil.getValue("appVersion"));
		return NONE;
	}

	public UseCouponInfoDTO getUseCouponInfo() {
		return useCouponInfo;
	}

	public void setUseCouponInfo(UseCouponInfoDTO useCouponInfo) {
		this.useCouponInfo = useCouponInfo;
	}

}
